# coding:utf-8

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
import json

from tornado.options import options
from tornado.web import RequestHandler

tornado.options.define("port", default=8000, type=int, help="run server on the given port")


class IndexHandler(RequestHandler):
    """请求处理类"""
    def set_default_headers(self):
        self.set_header("itcast", "python")
        print("set_default_headers")

    def initialize(self):
        print("initialize")

    def prepare(self):
        print("prepare")

    def get(self):
        print("get")
        self.write("hello")

    def post(self):
        self.set_header("itcast", "C")
        print("post")
        self.send_error(404)

    def on_finish(self):
        print("on_finish")

    def write_error(self, status_code, **kwargs):
        print("write_error")
        self.write("error")


if __name__ == "__main__":
    tornado.options.parse_command_line()
    settings = {
        "debug": True,
    }
    app = tornado.web.Application(
        [(r"/", IndexHandler)],
        **settings
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()